/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.statweb.action;

import cn.ac.iie.di.wx.statweb.utils.DateUtil;
import cn.ac.iie.di.wx.statweb.utils.PicCopy;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author Administrator
 */
public class WxStatWebAction {

    static Logger logger = Logger.getLogger(WxStatWebAction.class.getName());
    private String trunk;

    public String wx_rt() {
        HttpServletRequest request = ServletActionContext.getRequest();
        trunk = request.getParameter("trunk");
        return "success";
    }

    public String wx_daily() {
        return "success";
    }

    public String wx_weekly() {
        return "success";
    }

    public String wx_rt_msgAccess() {

        HttpServletRequest request = ServletActionContext.getRequest();
        String trunkval = request.getParameter("rt_msgdelay");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        try {
            PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
            String home_path = PicCopy.readProperties("wx.head") + DateUtil.getToDayDate_1() + PicCopy.readProperties("wx.rt.tail");
            if (trunkval.equals("yizhuang")) {//默认为数北
                home_path = PicCopy.readProperties("wx.head") + DateUtil.getToDayDate_1() + PicCopy.readProperties("wx.rt.tail");
                logger.info("亦庄");
            }
            File file = new File(home_path);
            File[] files = file.listFiles();
            String fileStrArray = "";
//        logger.info(System.getProperty("user.dir"));
        logger.info(home_path);
            for (int i = 0; i < files.length; i++) {
                String filename = files[i].getName();
//            String filePath = "E:\\ban-ycl-wx-stat-web\\src\\main\\webapp\\wx\\4\\";
                String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.wx.path") + "4/";
                File fileDir = new File(filePath);
                if (!fileDir.isDirectory()) {
//                logger.info(fileDir.isDirectory());
                    fileDir.mkdirs();
//                logger.info(filePath);
                } else {
//                logger.info(fileDir.isDirectory());
//                logger.info(filePath);
                }
                File f = new File(filePath + filename);
                if (f.exists()) {
                    if (f.delete()) {
                        f.createNewFile();
                    }
                    PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
                } else {
                    f.createNewFile();
                    logger.info("create file");
                    PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
                }
            }
            String[] fs = file.list();
            Arrays.sort(fs);
            for (int i = 0; i < fs.length; i++) {
                fileStrArray += fs[i] + ";";
            }
            out.print(fileStrArray);
            out.close();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "success";
    }

    public String wx_rt_userAccess() {
        return "success";
    }

    public String wx_dailyData() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String selectedDay = request.getParameter("selectedDay");
        String trunkval = request.getParameter("rt_msgdelay");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
        String home_path = PicCopy.readProperties("wx.head") + selectedDay + PicCopy.readProperties("wx.daily.tail");
        if (trunkval.equals("yizhuang")) {//默认为数北
            home_path = PicCopy.readProperties("wx.head") + selectedDay + PicCopy.readProperties("wx.daily.tail");
        }
        File file = new File(home_path);
        File[] files = file.listFiles();
        String fileStrArray = "";
        logger.info(home_path);
//        logger.info(files.length);
        for (int i = 0; i < files.length; i++) {
            String filename = files[i].getName();
//            String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
            String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.wx.path") + selectedDay + "/24/";
            File fileDir = new File(filePath);
            if (!fileDir.isDirectory()) {
//                logger.info(fileDir.isDirectory());
                fileDir.mkdirs();
                logger.info(filePath);
            } else {
//                logger.info(fileDir.isDirectory());
                logger.info(filePath);
            }
            File f = new File(filePath + filename);
            if (f.exists()) {
                if (f.delete()) {
                    f.createNewFile();
                }
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            } else {
                f.createNewFile();
//                logger.info("create file");
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            }
        }
        String[] fs = file.list();
        Arrays.sort(fs);
        for (int i = 0; i < fs.length; i++) {
            fileStrArray += fs[i] + ";";
        }
        out.print(fileStrArray);
        out.close();
        return "success";
    }

    public String wx_weeklyData() throws IOException {
        //wx_weekly_selectedDay
        HttpServletRequest request = ServletActionContext.getRequest();
        String selectedDay = request.getParameter("wx_weekly_selectedDay");
        String trunkval = request.getParameter("rt_msgdelay");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
        String home_path = PicCopy.readProperties("wx.head") + selectedDay + PicCopy.readProperties("wx.weekly.tail");
        if (trunkval.equals("yizhuang")) {//默认为数北
            home_path = PicCopy.readProperties("wx.head") + selectedDay + PicCopy.readProperties("wx.weekly.tail");
        }
        File file = new File(home_path);
        File[] files = file.listFiles();
        String fileStrArray = "";
//        logger.info(System.getProperty("user.dir"));
        logger.info(home_path);
        for (int i = 0; i < files.length; i++) {
            String filename = files[i].getName();
//            String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
            String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.wx.path") + selectedDay + "/168/";
            File fileDir = new File(filePath);
            if (!fileDir.isDirectory()) {
//                logger.info(fileDir.isDirectory());
                fileDir.mkdirs();
//                logger.info(filePath);
            } else {
//                logger.info(fileDir.isDirectory());
//                logger.info(filePath);
            }
            File f = new File(filePath + filename);
            if (f.exists()) {
                if (f.delete()) {
                    f.createNewFile();
                }
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            } else {
                f.createNewFile();
//                logger.info("create file");
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            }
        }
        String[] fs = file.list();
        Arrays.sort(fs);
        for (int i = 0; i < fs.length; i++) {
            fileStrArray += fs[i] + ";";
        }
        out.print(fileStrArray);
        out.close();
        return "success";
    }

    public String wx_rt_1113() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String wx_rt_1113 = request.getParameter("wx_rt_1113");//判断是数北数据还是亦庄数据
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
        String selectedDay = DateUtil.getToDayDate_1();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
        String home_path = PicCopy.readProperties("wx.1113.head") + selectedDay + PicCopy.readProperties("wx.1113.rt.tail");
        if (wx_rt_1113.equals("yizhuang")) {//默认为数北
            home_path = PicCopy.readProperties("wx.1113.head") + "yizhuang/" + selectedDay + PicCopy.readProperties("wx.1113.rt.tail");
        }
        File file = new File(home_path);
        File[] files = file.listFiles();
        String fileStrArray = "";
        logger.info(file.getPath());
//        logger.info(files.length);
        for (int i = 0; i < files.length; i++) {
            String filename = files[i].getName();
//            String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
            String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.wx.1113.path") + "4/";
            File fileDir = new File(filePath);
            if (!fileDir.isDirectory()) {
//                logger.info(fileDir.isDirectory());
                fileDir.mkdirs();
                logger.info(filePath);
            } else {
//                logger.info(fileDir.isDirectory());
                logger.info(filePath);
            }
            File f = new File(filePath + filename);
            if (f.exists()) {
                if (f.delete()) {
                    f.createNewFile();
                }
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            } else {
                f.createNewFile();
//                logger.info("create file");
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            }
        }
        String[] fs = file.list();
        Arrays.sort(fs);
        for (int i = 0; i < fs.length; i++) {
            fileStrArray += fs[i] + ";";
        }
        out.print(fileStrArray);
        out.close();
        return "success";
    }

    public String wx_1113_dailyData() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String selectedDay = request.getParameter("selectedDay");
        String wx_rt_1113 = request.getParameter("wx_rt_1113");//判断是数北数据还是亦庄数据
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
        String home_path = PicCopy.readProperties("wx.1113.head") + selectedDay + PicCopy.readProperties("wx.1113.daily.tail");
        if (wx_rt_1113.equals("yizhuang")) {//默认为数北
            home_path = PicCopy.readProperties("wx.1113.head") + "yizhuang/" + selectedDay + PicCopy.readProperties("wx.1113.daily.tail");
        }
        File file = new File(home_path);
//        File file = new File(PicCopy.readProperties("wx.1113.head") + selectedDay + PicCopy.readProperties("wx.1113.daily.tail"));
        File[] files = file.listFiles();
        String fileStrArray = "";
        logger.info(PicCopy.readProperties("wx.1113.head") + selectedDay + PicCopy.readProperties("wx.1113.daily.tail"));
//        logger.info(files.length);
        for (int i = 0; i < files.length; i++) {
            String filename = files[i].getName();
//            String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
            String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.wx.1113.path") + selectedDay + "/24/";
            File fileDir = new File(filePath);
            if (!fileDir.isDirectory()) {
//                logger.info(fileDir.isDirectory());
                fileDir.mkdirs();
                logger.info(filePath);
            } else {
//                logger.info(fileDir.isDirectory());
                logger.info(filePath);
            }
            File f = new File(filePath + filename);
            if (f.exists()) {
                if (f.delete()) {
                    f.createNewFile();
                }
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            } else {
                f.createNewFile();
//                logger.info("create file");
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            }
        }
        String[] fs = file.list();
        Arrays.sort(fs);
        for (int i = 0; i < fs.length; i++) {
            fileStrArray += fs[i] + ";";
        }
        out.print(fileStrArray);
        out.close();
        return "success";
    }

    public String wx_1113_weeklyData() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String selectedDay = request.getParameter("wx_weekly_selectedDay");
        String wx_rt_1113 = request.getParameter("wx_rt_1113");//判断是数北数据还是亦庄数据
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
        String home_path = PicCopy.readProperties("wx.1113.head") + selectedDay + PicCopy.readProperties("wx.1113.weekly.tail");
        if (wx_rt_1113.equals("yizhuang")) {//默认为数北
            home_path = PicCopy.readProperties("wx.1113.head") + "yizhuang/" + selectedDay + PicCopy.readProperties("wx.1113.weekly.tail");
        }
        File file = new File(home_path);
        File[] files = file.listFiles();
        String fileStrArray = "";
        logger.info(home_path);
//        logger.info(files.length);
        for (int i = 0; i < files.length; i++) {
            String filename = files[i].getName();
//            String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
            String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.wx.1113.path") + selectedDay + "/168/";
            File fileDir = new File(filePath);
            if (!fileDir.isDirectory()) {
//                logger.info(fileDir.isDirectory());
                fileDir.mkdirs();
                logger.info(filePath);
            } else {
//                logger.info(fileDir.isDirectory());
                logger.info(filePath);
            }
            File f = new File(filePath + filename);
            if (f.exists()) {
                if (f.delete()) {
                    f.createNewFile();
                }
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            } else {
                f.createNewFile();
//                logger.info("create file");
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            }
        }
        String[] fs = file.list();
        Arrays.sort(fs);
        for (int i = 0; i < fs.length; i++) {
            fileStrArray += fs[i] + ";";
        }
        out.print(fileStrArray);
        out.close();
        return "success";
    }

    public String getTrunk() {
        return trunk;
    }

    public void setTrunk(String trunk) {
        this.trunk = trunk;
    }

}
